import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
// import webLayout from '@/pages/webLayout.vue';
// const indexPage =
const routers = createRouter({
    history: createWebHashHistory(),//createWebHistory(),
    routes: [
        {
            path: "/loginPage", //登录
            component: () => import('@/pages/loginPage'),
        },
        {
            path: "/",//首页
            component: () => import('@/pages/homePage'),

        },
        {
            path: "/yingyongIndex",//应用中心
            component: () => import('@/pages/yingyongPage'),

        },
        {
            path: "/featuredApps",//精选应用
            component: () => import('@/pages/featuredAppsPage'),

        },
        {
            path: "/yingyongDetail/:id",//应用中心 详情
            component: () => import('@/pages/yingyongDetail'),

        },
        {
            path: "/statistics",//统计仪表盘
            component: () => import('@/pages/statistics/StatisticsDashboard'),

        },
        {
            path: "/kaifa",//开发者中心
            component: () => import('@/pages/kaifaPage'),
            children: [
                {
                    path: "/kaifaIndex",//开发者中心 概览
                    component: () => import('@/pages/kaifaManage/kaifaIndex.vue'),
                },
                {
                    path: "/kaifaForm",//开发者中心 表单
                    component: () => import('@/pages/kaifaManage/kaifaForm'),
                },
                {
                    path: "/accountList",//账号与安全
                    component: () => import('@/pages/kaifaManage/accountList'),
                },
                {
                    path: "/scenarioList",//应用场景管理
                    component: () => import('@/pages/kaifaManage/scenarioList'),
                },
            ]
        },
        {
            path: "/manageList",
            component: () => import('@/pages/webLayout'),
            children: [
                // {
                //     path: "/",
                //     component: () => import('@/pages/homePage'),
                // },
                {//增删改查页面
                    path: "/manageList",
                    component: () => import('@/pages/manage/manageList'),
                },
                {//增删改查页面
                    path: "/manageDetail/:id",
                    component: () => import('@/pages/manage/manageDetail'),
                },
            ]
        },

    ]
});

// 调试时放开

import store from '@/store/index.js';
import { reportDetail } from "@/assets/api/api";
import { ElMessage } from 'element-plus';
routers.beforeEach((to, from, next) => {
    // console.log(to.path)
    store.commit('changeActivePath', to.path);
    
    // 权限控制：应用场景管理页面仅管理员可访问
    if (to.path === '/scenarioList') {
        // 检查用户是否为管理员
        const isAdmin = store.state.isAdmin;
        if (!isAdmin) {
            ElMessage.error('权限不足，仅管理员可访问应用场景管理');
            next('/kaifa');
            return;
        }
    }
    
    next();
    //   let loginFlag = getCookie("loginFlag");
    //   if (to.path != "/login" && to.path != "/login_dy" && loginFlag == "") {
    //     if (from.fullPath.indexOf('_dy') > -1) {
    //       router.replace({ path: "/login_dy" });
    //     } else {
    //       router.replace({ path: "/login" });
    //     }
    //   } else {
    //     if (to.meta.title) {
    //       document.title = to.meta.title;
    //     }
    //     next();
    //   }
});
// 调试时放开-----------end
// router.afterEach(function(transition) {
//     if (transition.path == '/') {
//         location.reload()
//             // router.replace({ name: 'login' });
//             // let menulist = store.state.menulist;
//     }
// })

export default routers;
